# Functions for validating xml and handlers for parsing into ProtocolApplications 
###############################################################################

#' Validates an XML document and submits any issues to the log.
#' 
#' If the validation fails or generates warnings these will be written to the log, and
#' the pipeline execution is halted.
#' @param document The xml document
#' @return invisible TRUE if the validation did not generate any warnings or errors. 
validateXml <- function(document)
{
    schema <- drDobus:::protocolSchema
    validationResult <- xmlSchemaValidate(schema=drDobus:::pipelineSchema, doc=document)
    if (validationResult$status != 0)
    {
        if (validationResult$status == -1)
            submitToLog("The 3rd party library libxml failed with an internal error. Try updating to current Version.", "Error") 
        
        theWarnings <- paste(validationResult$warnings,collapse=",")
        theErrors <- paste(validationResult$errors,collapse=",")
        theMessage <- paste("The validation of a pipeline description failed with the following warnings or errors.",
                theWarnings,
                theErrors) 
        submitToLog(theMessage, "Error")
    }
    invisible(TRUE)
}

